from django.db import models

from car.models import Car
from user.models import User


# Create your models here.

class Coupon(models.Model):
    name = models.CharField(max_length=50, verbose_name='优惠券名称')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    type = models.CharField(max_length=50, blank=True, null=True, verbose_name='优惠类型')
    rule = models.TextField(blank=True, null=True, verbose_name='优惠规则')
    start_time = models.DateTimeField(verbose_name='开始时间')
    end_time = models.DateTimeField(verbose_name='结束时间')
    status = models.IntegerField(default=0, verbose_name='优惠状态')
    max_price = models.IntegerField(verbose_name='满金额')
    price = models.IntegerField(verbose_name='优惠金额')
    checked = models.IntegerField(default=0, verbose_name='是否使用')

    def __str__(self):
        return self.user.username

    class Meta:
        db_table = 'coupon'


class Order(models.Model):
    order_number = models.CharField(max_length=255, unique=True, verbose_name='订单编号')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    creation_date = models.DateTimeField(verbose_name='创建日期')
    total_amount = models.IntegerField(verbose_name='总金额')
    discount_amount = models.IntegerField(default=0, verbose_name='优惠金额')
    actual_payment = models.IntegerField(verbose_name='实际支付金额')
    address = models.CharField(max_length=255, verbose_name='地址')
    coupon = models.ForeignKey(Coupon, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='优惠券')
    paytype = models.CharField(max_length=50, verbose_name='支付方式')
    order_status = models.CharField(max_length=50, default='pending', verbose_name='订单状态')

    def __str__(self):
        return self.order_number

    class Meta:
        db_table = 'order'


class OrderDetail(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name='订单')
    goods = models.ForeignKey(Car, on_delete=models.CASCADE, verbose_name='商品')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    quantity = models.IntegerField(verbose_name='数量')
    image = models.CharField(max_length=255, verbose_name='图片')

    def __str__(self):
        return self.order.order_number

    class Meta:
        db_table = 'order_detail'
